package com.abou.mvc.controller.customer.product.detail;

import java.util.ArrayList;
import java.util.List;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.abou.constant.product.ECategory;
import com.abou.constant.product.EProductType;
import com.abou.model.common.product.Product;
import com.abou.model.common.product.search.SearchProductsRequest;
import com.abou.model.customer.LastConsultedProduct;
import com.abou.mvc.controller.ACommonController;
import com.abou.mvc.controller.ControllerUtils;

/**
 * @author kaka
 */
@SessionAttributes("lastConsultedProducts")
public abstract class AProductDetailController extends ACommonController {
  
  protected EProductType productType;
  protected ECategory category;
  
  /**
   * @param viewName
   * @param exPrevViewName
   */
  public AProductDetailController(String viewName, String exPrevViewName) {

    super(viewName, exPrevViewName);
  }
  
  /**
   * @return Creates a search product request
   */
  protected SearchProductsRequest createProductsRequest() {

    SearchProductsRequest searchRequest = new SearchProductsRequest();
    searchRequest.setProductType(productType);
    searchRequest.setCategory(category);
    
    return searchRequest;
  }
  
  /**
   * @param productId
   */
  @RequestMapping(method = RequestMethod.GET)
  public String showPage(Integer productId, ModelMap model) {

    // getting the product
    Product product = getProductManager().getProduct(productId.intValue());
    model.put(ControllerUtils.PRODUCT_ATT, product);
    
    // updating the last consulted products list
    updateLastProduct(model, product);
    
    return getViewName();
  }
  
  /**
   * @param model
   * @param product
   */
  @SuppressWarnings("unchecked")
  private void updateLastProduct(ModelMap model, Product product) {

    // getting the last consulted products
    List<LastConsultedProduct> lastProducts =
        (List<LastConsultedProduct>) model.get(ControllerUtils.LAST_PRODUCTS_ATT);
    
    if (lastProducts == null) {
      
      lastProducts = new ArrayList<LastConsultedProduct>();
    }
    
    // adding the in the last consulted products list
    LastConsultedProduct lastProduct =
        new LastConsultedProduct(
            product.getId(), product.getImagePath1(), product.getName(),
            product.getPrice(), getViewName()
                + ControllerUtils.VIEW_NAME_SUFFIX + ControllerUtils.PARAMS_DEL
                + "productId" + ControllerUtils.PARAMS_VAL_SEP
                + product.getId());
    
    if (!lastProducts.contains(lastProduct)) {
      
      // only 5 last products
      if (lastProducts.size() == 5) {
        
        lastProducts.remove(0);
      }
      
      lastProducts.add(lastProduct);
    }
    
    model.put(ControllerUtils.LAST_PRODUCTS_ATT, lastProducts);
  }
  
}
